﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Vit.Common;
using Vit.Common.Collections;
using Vit.Common.Helpers;
using Vit.Web;
using Vit.Web.Models;
using Vit.Web.Providers;
using Vit.Web.Mvc.Results;
using Vit.Web.Helpers;

namespace Vit.AdminModule.Areas.Admin.Controllers
{
    public class RoleController : Vit.Web.Mvc.Controllers.DataControllerBase<Vit.Web.Models.Role>
    {
        [HttpGet]
        public override ActionResult Index(string id)
        {
            var roles = this.DataProvider.GetAll().Select(o => new SelectListItem() { Text = o.Name, Value = o.Name });
            SetViewTitle("Index");
            return View(roles);
        }

        [HttpGet]
        [OutputCache(Duration = 3600, VaryByParam = "id")]
        public ActionResult Permissions(string id)
        {
            object[] keys = UnFormatKeys(id);
            var role = this.DataProvider.GetByKeys(keys);
            ViewBag.Role = role;
            var items = MenuHelper.GetMenuItems(1).Where(it => !string.IsNullOrEmpty(it.PermissionPattern));
            var tree = HierarchyHelper.Build<NavigationItem, TreeNode<NavigationItem>>(items,
                o =>
                {
                    TreeNode<NavigationItem> node = new TreeNode<NavigationItem>(o.Name, o.Caption, o);
                    node.Selected = role.Permissions.Any(rp => rp.Pattern == o.PermissionPattern);
                    node.Value = o.PermissionPattern;
                    return node;
                },
                o => o.Items.Where(it => !string.IsNullOrEmpty(it.PermissionPattern)),
                (p, c) => { p.AddNode(c); });

            return View(tree);
        }

        [HttpPost]
        public ActionResult Permissions(FormCollection form)
        {
            object[] keys = UnFormatKeys(form["RoleID"]);
            var role = this.DataProvider.GetByKeys(keys);
            if (role != null)
            {
                role.Permissions.Clear();
                foreach (string fid in form.AllKeys.Where(o => o.StartsWith("cb_")))
                {
                    string pattern = form[fid].ToLower();
                    if (role.Permissions.Any(o => o.Pattern == pattern))
                        continue;
                    var permission = new Permission()
                    {
                        Pattern = pattern,
                        RoleName = role.Name
                    };
                    role.Permissions.Add(permission);
                }
                this.DataProvider.Modify(role);
            }
            return Refresh();
        }

        protected override void SetViewTitle(string viewName)
        {
            string pageTitle = string.Empty;
            switch (viewName)
            {
                default:
                case "Index":
                    pageTitle = "Data_ListPageTitle";
                    break;
                case "Edit":
                    pageTitle = "Data_EditPageTitle";
                    break;
                case "Detail":
                    pageTitle = "Data_DetailPageTitle";
                    break;
                case "Create":
                    pageTitle = "Data_CreatePageTitle";
                    break;

            }
            ViewBag.Title = string.Format(Locale.GetString("Vit.Web", pageTitle), Locale.GetString(typeof(Role), "Role"));
        }

        [HttpGet]
        public ActionResult RoleExists(string name)
        {
            bool exists = this.DataProvider.GetAll().Any(o => o.Name == name);
            return Json(!exists, JsonRequestBehavior.AllowGet);
        }
    }
}
